//
//  ELHomeView.m
//  ELProject
//
//  Created by Yuki on 2021/4/13.
//

#import "ELHomeView.h"
#import "ElHomeTopView.h"///顶部界面
#import "ELHomeNavView.h"///导航栏
@interface ELHomeView ()<UITableViewDelegate,UITableViewDataSource>
/**  顶部界面  **/
@property (nonatomic , strong)ElHomeTopView  *topView;
/**  导航栏  **/
@property (nonatomic , strong)ELHomeNavView  *navView;
/**  列表  **/
@property (nonatomic , strong)UITableView  *mainList;
/**  高度  **/
@property (nonatomic , assign)CGFloat  topViewHeight;
@end

@implementation ELHomeView

#pragma mark - --------- 创建界面  ---------
- (void)_setupView{
    [super _setupView];
    
    self.topViewHeight = 50*Scale() + NavgitionHeight();
    
    /**  添加列表  **/
    [self addSubview:self.mainList];
    [self.mainList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.mas_equalTo(0);
    }];
    self.mainList.dataSource = self;
    self.mainList.delegate = self;
    self.mainList.contentInset = UIEdgeInsetsMake(self.topViewHeight, 0, 0, 0);
    self.mainList.scrollIndicatorInsets = self.mainList.contentInset;
    
    
    [self.mainList.superview layoutIfNeeded];
    /**  导航栏  **/
    self.topView.frame =CGRectMake(0, -self.topViewHeight, ScreenWith(), self.topViewHeight);
    [self.mainList addSubview:self.topView];
    
    /**  忽略高度  **/
    self.mainList.contentOffset = CGPointMake(0, -self.topViewHeight);
    
    /**  导航栏  **/
    [self addSubview:self.navView];
    self.navView.alpha = 0;
    [self.navView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(NavgitionHeight());
    }];
    
    self.mainList.mj_header = [ZLRefreshHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.mainList.mj_header endRefreshing];
        });
    }];

    
}

/**  界面滑动  **/
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offsetY = scrollView.contentOffset.y;
    NSLog(@"%f",offsetY);
    
    if (offsetY <= -self.topViewHeight) {
        self.topView.frame = CGRectMake(0, offsetY, ScreenWith(), self.topViewHeight);
    }
    

    
    CGFloat height = -offsetY - NavgitionHeight() - 50*Scale();
    CGFloat scale = height / 5.0f;
    
    if (scale >= 0) {
        scale = 0;
    }
    if (scale <= -10) {
        scale = -10;
    }
    self.topView.flexScale = scale;
    
    CGFloat navAlpha = scale/10.0;
    
    if (navAlpha == -1.0) {
        navAlpha = -1.0;
        self.navView.alpha = -navAlpha;
    }else{
        self.navView.alpha = 0;
    }
    
        
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 10;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.backgroundColor = UIColor.yellowColor;
    return cell;
}

#pragma mark - --------- lazy  ---------
- (ELHomeNavView *)navView{
    if (!_navView) {
        _navView = [[ELHomeNavView alloc]init];
    }
    return _navView;
}
- (ElHomeTopView *)topView{
    if (!_topView) {
        _topView = [[ElHomeTopView alloc]init];
    }
    return _topView;
}

/**  列表  **/
- (UITableView *)mainList{
    if (!_mainList) {
        _mainList = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _mainList.backgroundColor = ColorHex(0xffffff, 1);
        FitiOS11(_mainList);
    }
    return _mainList;
}
@end
